<?php

namespace app\admin\controller;

use app\admin\service\RbacService;

/**
 * 权限管理
 * Class Rbac
 * @package app\admin\controller
 */
class Rbac extends CommonController
{

    /**
     * 用户管理
     * @return
     */
    public function admin()
    {
        if ($this->isAjax()) {
            $request = $this->request;
            $list = RbacService::getAdminList($request);
            return $this->success($list);
        }

        return view('admin/index');
    }

    /**
     * 编辑管理员
     * @return
     */
    public function editadmin()
    {
        $request = $this->request;
        if ($this->isAjax()) {
            $formData = $request['form'];
            if (RbacService::isAdmin($this->admin_id) == 1)
            {
                $list = RbacService::editAdmin($formData);
            } else {
                return $this->error(1, '您没有权限!');
            }

            return $this->success($list);
        }

        $roles = RbacService::getAdminRoleList();
        $data = RbacService::getAdmin($request);

        return view('admin/edit', ['data' => $data, 'roles' => $roles]);
    }

    /**
     * 删除管理员
     * @return \think\response\Json
     */
    public function deladmin()
    {
        $request = $this->request;
        if (RbacService::isAdmin($this->admin_id) == 1)
        {
            RbacService::delAdmin($request['ids']);
        } else {
            return $this->error(1, '您没有权限!');
        }

        return $this->success();
    }

    /**
     * 管理员分组
     * @return
     */
    public function role()
    {
        if ($this->isAjax()) {
            $list = RbacService::getRoleList();
            return $this->success($list);
        }

        return view('role/index');
    }

    /**
     * 编辑分组
     * @return
     */
    public function editrole()
    {
        $request = $this->request;
        if ($this->isAjax()) {
            $formData = $request['form'];
            $list = RbacService::editRole($formData);
            return $this->success($list);
        }

        $data = RbacService::getRole($request);
        return view('role/edit', ['data' => $data]);
    }

    /**
     * 删除分组
     * @return \think\response\Json
     */
    public function delrole()
    {
        $request = $this->request;
        RbacService::delRole($request['ids']);
        return $this->success();
    }

    /**
     * 分组权限
     * @return \think\response\Json
     */
    public function grouprules()
    {
        $request = $this->request;
        $groupId = $request['gid'] ?? 0;
        if ($this->isAjax()) {
            $data = RbacService::getRuleList($groupId);
            return $this->success($data);
        }
        return view('role/rules', ['id' => $groupId]);
    }

    /**
     * 分组权限
     * @return \think\response\Json
     */
    public function saverules()
    {
        $request = $this->request;
        $data['id'] = $request['gid'] ?? 0;
        $data['rules'] = implode(',', $request['ids']);
        $data = RbacService::editRole($data);
        return $this->success($data);
    }


}
